package cn.stylefeng.guns.core.util;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

/**
 * @Author: Xiazt
 * @Date: 2019/4/18 11:44
 */
public class CodeGenerator {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //此处设置你的包结构
        String packageName = "cn.stylefeng.guns.modular.system";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        System.out.println("请输入表名：");
        String tableName = scanner.next();
        generateByTables(serviceNameStartWithI, packageName, tableName);
    }


    private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        //指数据库地址
        String dbUrl = "jdbc:mysql://94.191.76.216:3306/guns?serverTimezone=GMT";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        String dir = System.getProperty("user.dir");
        //String dir = "D:\\newJava";
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                //数据库用户名
                .setUsername("root")
                //数据库密码
                .setPassword("baizeadmin2018")
                //数据库连接驱动
                .setDriverName("com.mysql.cj.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                //表名前缀
                .setTablePrefix("")
                .setEntityBooleanColumnRemoveIsPrefix(true)
                .setNaming(NamingStrategy.underline_to_camel)
                //修改替换成你需要的表名，多个表名传数组。如果只有一张表，则不需要动
                .setInclude(tableNames);
        config.setActiveRecord(false)
                .setEnableCache(false)
                //作者
                .setAuthor("zfy")
                .setOutputDir(dir + "\\src\\main\\java")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
            config.setMapperName("%sMapper");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setMapper("mapper")
                                .setXml("mapper\\mapping")
                                .setEntity("entity")
                ).execute();
    }
}
